package com.apotik.legenda.dialog;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JDialog;

import com.apotik.legenda.model.SalesTrxObat;
import com.apotik.legenda.observer.PosObserver;
import com.apotik.legenda.panel.PosPanel;

import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.FlowLayout;
import java.util.HashMap;

public class EditTrxObatScDialog extends JDialog implements KeyListener, ActionListener {
	private static final long serialVersionUID = 1L;

	private PosObserver observer;
	private int rowIndex;
	private SalesTrxObat salesTrxObat;
	private JTextField textFieldSc;
	private JTextField textFieldBarang;
	private JButton btnSubmit;
	private JButton btnClose;
	
	public EditTrxObatScDialog(PosObserver observer, int rowIndex, SalesTrxObat salesTrxObat) {
		this.observer = observer;
		this.rowIndex = rowIndex;
		this.salesTrxObat = salesTrxObat;
		
		setTitle("Edit Quantity");
		setSize(350, 170);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		addKeyListener(this);
		
		JPanel panel = new JPanel();
		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new MatteBorder(1, 1, 1, 1, (Color) Color.GRAY));
		panel.add(panel_1);
		panel_1.setLayout(new BorderLayout(0, 0));
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new EmptyBorder(3, 3, 3, 3));
		panel_1.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(new GridLayout(0, 2, 0, 5));
		
		JLabel lblBarang = new JLabel("Barang");
		lblBarang.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_2.add(lblBarang);
		
		textFieldBarang = new JTextField();
		textFieldBarang.setEnabled(false);
		textFieldBarang.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_2.add(textFieldBarang);
		textFieldBarang.setColumns(10);
		textFieldBarang.setText(salesTrxObat.getProductName());
		
		JLabel lblSc = new JLabel("SC (Rp)");
		lblSc.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_2.add(lblSc);
		
		textFieldSc = new JTextField();
		textFieldSc.setFont(new Font("Tahoma", Font.PLAIN, 13));
		panel_2.add(textFieldSc);
		textFieldSc.setColumns(10);
		textFieldSc.setText(Integer.toString(salesTrxObat.getAmountSC().intValue()));
		
		JLabel lblNewLabel = new JLabel("");
		panel_2.add(lblNewLabel);
		
		JPanel panel_3 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_3.getLayout();
		flowLayout.setAlignment(FlowLayout.LEFT);
		panel_2.add(panel_3);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.addKeyListener(this);
		btnSubmit.addActionListener(this);
		this.getRootPane().setDefaultButton(btnSubmit);
		panel_3.add(btnSubmit);
		
		btnClose = new JButton("Close");
		btnClose.addKeyListener(this);
		btnClose.addActionListener(this);
		panel_3.add(btnClose);
	}
	
	private void btnSubmitAction(){
		String scGet = textFieldSc.getText().trim().equalsIgnoreCase("") ? "0":textFieldSc.getText().trim();
		int rowIndex = this.rowIndex;
		SalesTrxObat sto = this.salesTrxObat;
		int scInt = Integer.parseInt(scGet);
		
		HashMap<String, Object> hm = new HashMap<>();
		hm.put("update_for", PosPanel.UPDATE_SC);
		hm.put("rowIndex", rowIndex);
		hm.put("salesTrxObat", sto);
		hm.put("scInt", scInt);
		
		observer.updateProduct(hm);
		
		this.setVisible(false);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		
		if(source==btnSubmit){
			btnSubmitAction();
		}else if(source==btnClose){
			this.setVisible(false);
		}
		
	}

	@Override
	public void keyPressed(KeyEvent ke) {
		if(ke.getKeyCode()==KeyEvent.VK_ESCAPE){
			this.setVisible(false);
		}
		
		Object source = ke.getSource();
		if(source==btnSubmit){
			if(ke.getKeyCode()==KeyEvent.VK_ENTER){
				btnSubmitAction();
			}
		}else if(source==btnClose){
			if(ke.getKeyCode()==KeyEvent.VK_ENTER){
				this.setVisible(false);
			}
		}
		
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
